package com.jintian.smart.kernel.common.expr.op.define;

import com.jintian.smart.kernel.common.expr.datameta.BaseDataMeta;
import com.jintian.smart.kernel.common.expr.datameta.Constant;
import com.jintian.smart.kernel.common.expr.op.Operator;
import com.jintian.smart.kernel.common.util.ArrayUtil;
import com.jintian.smart.kernel.core.expr.IllegalExpressionException;

import java.util.Collection;

/**
 * 算术取负操作
 */
public class Op_IN extends Op_Base {
    public Op_IN() {
        super(Operator.IN);
    }

    public Constant execute(Constant[] args) throws IllegalExpressionException {
        Object[] params = this.getParameterValues(args);

        if (params[0] == null || params[1] == null) {
            throw new IllegalArgumentException("操作符\"" + this.getOperator().getToken() + "\"不能比较空值");
        }

        if (params[1].getClass().isArray()) {
            return new Constant(BaseDataMeta.DataType.DATATYPE_BOOLEAN, ArrayUtil.indexOf((Object[]) params[1], params[0]) >= 0);
        } else if (params[1] instanceof Collection) {
            return new Constant(BaseDataMeta.DataType.DATATYPE_BOOLEAN,
                    ((Collection<?>) params[1]).contains(params[0]));
        } else {
            throw new IllegalArgumentException("操作符\"" + this.getOperator().getToken() + "\"参数类型错误");
        }

    }

    public Constant verify(int opPositin, BaseDataMeta[] args) throws IllegalExpressionException {
        this.checkParameterCount(opPositin, args);
        return new Constant(BaseDataMeta.DataType.DATATYPE_BOOLEAN, Boolean.FALSE);
    }

}
